package com.ruoyi.api.file;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.result.ResultEnum;
import com.ruoyi.result.ResultView;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RequestMapping("/api/file/upload")
@RestController
public class FileUploadController {
  @PostMapping(value = "/{type}")
  @ResponseBody
  public ResultView uploadAvatar(@RequestParam("file") MultipartFile file,@PathVariable String type){
    try {
      String baseDir=Global.getUploadPath()+"/"+type;
      String path = FileUploadUtils.upload(baseDir, file);
      return ResultView.ok(path);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return ResultView.error(ResultEnum.OPR_FAILED);
  }

}
